<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>ShardingSphere-SQL 解析 Issue 处理流程 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="ShardingSphere-SQL 解析 Issue 处理流程这是之前给社区写的 SQL 解析 Issue 的处理流程，可以帮助社区用户快速参与到 ShardingSphere-SQL 解析当中。 ShardingSphere SQL 解析 issue 列表 Issue 背景说明当前 Issue 使用自定义的爬虫脚本从对应的数据库官网上抓取 SQL Cases，然后交给 ShardingSphe">
<meta property="og:type" content="article">
<meta property="og:title" content="ShardingSphere-SQL 解析 Issue 处理流程">
<meta property="og:url" content="https://flyingzc.github.io/articles/shardingsphere/ShardingSphere-SQL%E8%A7%A3%E6%9E%90Issue%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="ShardingSphere-SQL 解析 Issue 处理流程这是之前给社区写的 SQL 解析 Issue 的处理流程，可以帮助社区用户快速参与到 ShardingSphere-SQL 解析当中。 ShardingSphere SQL 解析 issue 列表 Issue 背景说明当前 Issue 使用自定义的爬虫脚本从对应的数据库官网上抓取 SQL Cases，然后交给 ShardingSphe">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2024-03-04T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.214Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="ShardingSphere">
<meta property="article:tag" content="SQL-Parse">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "ShardingSphere-SQL 解析 Issue 处理流程",
  "url": "https://flyingzc.github.io/articles/shardingsphere/ShardingSphere-SQL%E8%A7%A3%E6%9E%90Issue%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2024-03-04T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.214Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/shardingsphere/ShardingSphere-SQL%E8%A7%A3%E6%9E%90Issue%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'ShardingSphere-SQL 解析 Issue 处理流程',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">ShardingSphere-SQL 解析 Issue 处理流程</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">ShardingSphere-SQL 解析 Issue 处理流程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-03-04T16:00:00.000Z" title="发表于 2024-03-05 00:00:00">2024-03-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.214Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/ShardingSphere/">ShardingSphere</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="ShardingSphere-SQL-解析-Issue-处理流程"><a href="#ShardingSphere-SQL-解析-Issue-处理流程" class="headerlink" title="ShardingSphere-SQL 解析 Issue 处理流程"></a>ShardingSphere-SQL 解析 Issue 处理流程</h1><p>这是之前给社区写的 SQL 解析 Issue 的处理流程，可以帮助社区用户快速参与到 ShardingSphere-SQL 解析当中。</p>
<p><a target="_blank" rel="noopener" href="https://github.com/apache/shardingsphere/issues?q=is:open+is:issue+author:FlyingZC+label:%22in:+SQL+parse%22+label:%22good+first+issue%22">ShardingSphere SQL 解析 issue 列表</a></p>
<h2 id="Issue-背景说明"><a href="#Issue-背景说明" class="headerlink" title="Issue 背景说明"></a>Issue 背景说明</h2><p>当前 Issue 使用自定义的爬虫脚本从对应的数据库官网上抓取 SQL Cases，然后交给 ShardingSphere 的解析引擎进行解析。对于解析失败的 SQL Case，每 3~5 条 SQL 作为一个 Issue。</p>
<ol>
<li>由于是通过爬虫抓取的，所以不保证所有 SQL 本身就是正确的，请按照如下流程处理该 PR。</li>
<li>有些 SQL Case 可能已经在其他 PR 里被修复，对于已经可以执行的 Case 直接留言忽略即可。</li>
<li>如果该 SQL Case 已经可以成功运行，没有任何代码改动，那么不需要添加对应的 Test 断言文件。</li>
</ol>
<h2 id="总体流程"><a href="#总体流程" class="headerlink" title="总体流程"></a>总体流程</h2><ol>
<li>如果你打算参与该 Issue 的修复，欢迎在 Issue 下方留言，会有社区同学分配该 Issue。</li>
</ol>
<p>比如留言：Hi, please assign this issue to me. Thank you!</p>
<ol start="2">
<li>当认领到 Issue 之后，请在对应数据库的官网上查看该 SQL 的相关语法，并在对应的数据库上执行该 SQL 确保 SQL 本身语法的正确性。</li>
</ol>
<p>举个例子，对于 Issue: <a target="_blank" rel="noopener" href="https://github.com/apache/shardingsphere/issues/30305">https://github.com/apache/shardingsphere/issues/30305</a></p>
<p>总共包含 3 条有问题的 SQL Cases。</p>
<p>可以通过 case 下方的 link 查看该条 SQL 在数据库官网上对应的出处。</p>
<p><img src="/images/img-20240304182808-1.jpg"></p>
<p>点开官网链接后，查看相关 SQL 语法。如果链接里不包含对应的 SQL 语法，此时需要您自行搜索相关 SQL 语法。</p>
<p>比如此处官网对应的语法示例如下：</p>
<p><img src="/images/img-20240304182808-2.jpg"></p>
<p>接着把上述有问题的 SQL CASE 放到数据库里执行（可以使用对应数据库的 Docker 镜像快速启动对应数据库，然后使用您熟悉的客户端进行连接），确保 SQL 语法本身没有问题。</p>
<p><img src="/images/img-20240304182809-3.jpg"></p>
<h3 id="ANTLR-语法解析问题修复"><a href="#ANTLR-语法解析问题修复" class="headerlink" title="ANTLR 语法解析问题修复"></a>ANTLR 语法解析问题修复</h3><p>确定 SQL 语法正确之后，可以在 ShardingSphere 里验证并修复该语法解析问题。</p>
<p>如果您使用的是 Intellij IDEA，需要先安装 ANTLR 插件。</p>
<p><img src="/images/img-20240304182809-4.jpg"></p>
<p>当插件安装完成之后，在 IDEA 底部菜单栏中找到 ANTLR Preview 菜单。</p>
<p><img src="/images/img-20240304182809-5.jpg"></p>
<p>下面打开第一条 SQL <code>CREATE TABLE</code> 对应的 <code>.g4</code> 文件。目前<code>.g4</code> 规则已经按照 SQL 类型进行分类，比如<code>CREATE TABLE</code> 语法规则在 <code>DDLStatement.g4</code>文件中。</p>
<p>在 <code>DDLStatement.g4</code> 文件里 <code>createTable</code> 这条规则上右键-&gt;Test Rule Xxx。然后将有问题的 SQL 粘贴到底部的 ANTLR Preview 窗口中。</p>
<p><img src="/images/img-20240304182809-6.jpg"></p>
<p>此时可以看到 ANTLR 解析报错的信息，根据报错信息对照数据库官方语法尝试修复 <code>.g4</code> 文件，直到 SQL 可以被 ANTLR 正确的解析。</p>
<p>可以在修复<code>.g4</code> 语法过程中，实时通过执行 <code>Test Rule Xxx</code> 在 ANTLR Preview 窗口中观察语法问题是否已经被成功修复。</p>
<p>当 ANTLR Preview 窗口不再有报错信息，表示 ANTLR 已经可以正确解析该条 SQL 了。</p>
<p><img src="/images/img-20240304182810-7.jpg"></p>
<h3 id="Visitor-问题修复"><a href="#Visitor-问题修复" class="headerlink" title="Visitor 问题修复"></a>Visitor 问题修复</h3><p>ANTLR 将 SQL 解析成抽象语法树之后，ShardingSphere 会通过 Visitor 访问抽象语法树，提取所需的信息。</p>
<p>如果需要提取 Segment，需要先在 <code>shardingsphere-parser</code> 模块下，执行 <code>mvn -T 2C clean install -DskipTests</code> 编译整个 parser 模块。</p>
<p>然后按需重写 <code>SQLStatementVisitor</code> 里对应的 visit 方法，提取对应的 Segment。</p>
<p>参考 <a target="_blank" rel="noopener" href="https://github.com/apache/shardingsphere/pull/30258">https://github.com/apache/shardingsphere/pull/30258</a></p>
<p><img src="/images/img-20240304182811-8.jpg"></p>
<h3 id="添加断言测试文件"><a href="#添加断言测试文件" class="headerlink" title="添加断言测试文件"></a>添加断言测试文件</h3><p>当上面 SQL 解析问题修复完毕之后，需要添加对应的 Test，步骤如下：</p>
<ol>
<li>在 <code>sql/supported</code> 目录下添加对应的 <code>sql-case</code>。</li>
<li>在 shardingsphere-test-it-parser 模块的 case 目录下添加 case 断言。</li>
</ol>
<p><img src="/images/img-20240304182811-9.jpg"></p>
<ol start="3">
<li>运行 <code>org.apache.shardingsphere.test.it.sql.parser.internal.InternalSQLParserIT</code>，确保 SQL Parser IT 可以正常运行。</li>
</ol>
<p><code>InternalSQLParserIT</code> 对于每种数据库方言，提供了对应实现。比如此处运行 <code>InternalSQLServerParserIT</code>。</p>
<p><img src="/images/img-20240304182811-10.jpg"></p>
<p>当 SQL Parser IT 运行成功之后，即可提交 PR。</p>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/shardingsphere/ShardingSphere-SQL%E8%A7%A3%E6%9E%90Issue%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.html">https://flyingzc.github.io/articles/shardingsphere/ShardingSphere-SQL%E8%A7%A3%E6%9E%90Issue%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/ShardingSphere/">ShardingSphere</a><a class="post-meta__tags" href="/tags/SQL-Parse/">SQL-Parse</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/articles/shardingsphere/ShardingSphere-Narayana-XA%E4%BA%8B%E5%8A%A1%E4%B8%8D%E5%9B%9E%E6%BB%9A%E9%97%AE%E9%A2%98%E5%AE%9A%E4%BD%8D.html" title="ShardingSphere Narayana XA 事务不回滚问题定位"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-29</div><div class="info-item-2">ShardingSphere Narayana XA 事务不回滚问题定位</div></div><div class="info-2"><div class="info-item-1">ShardingSphere Narayana XA 事务不回滚问题定位问题背景用户反馈，在使用 ShardingSphere + Narayana 执行 XA 事务时，发生报错：java.sql.SQLException: javax.transaction.RollbackException: TransactionImple.enlistResource - ARJUNA016064: The transaction is in an invalid state!。这个报错的含义简单来说就是这个事务已经在其他地方被标记成只可回滚了，不能再进行后续其他的操作。报错信息如下： 12345678910111213141516171819202122Caused by: java.sql.SQLException: javax.transaction.RollbackException: TransactionImple.enlistResource - ARJUNA016064: The transaction is in an invalid state!        at...</div></div></div></a><a class="pagination-related" href="/articles/shardingsphere/ShardingSphere-XA%E4%BA%8B%E5%8A%A1%E6%97%A0%E6%B3%95%E6%81%A2%E5%A4%8D%E9%97%AE%E9%A2%98.html" title="ShardingSphere-XA事务无法恢复问题定位"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-28</div><div class="info-item-2">ShardingSphere-XA事务无法恢复问题定位</div></div><div class="info-2"><div class="info-item-1">ShardingSphere-XA事务无法恢复问题定位问题背景在测试 ShardingSphere-Proxy 使用达梦数据库开启 XA 事务进行压测期间，kill 掉达梦数据库，等一段时间再重启达梦数据库，最终未完成的 XA 事务无法恢复，数据产生不一致。查看达梦上的 xid 一直有残留，没有被处理。使用 ShardingSphere-Proxy 结合其他主流数据库进行测试时无该问题。 通过日志可以看到在压测期间，挂掉达梦数据库之后，有大量连接报错。节选错误日志如下：比如下面这段日志，在执行 xa end 操作时，因为达梦数据库连接不上，达梦驱动报错如下： 1234567891011121314151617181920212223242526272829[WARN ] 2024-01-29 11:11:19.867 [Connection-82-ThreadExecutor] com.arjuna.ats.jta - ARJUNA016041: prepare on &lt; formatId=131077, gtrid_length=64, bqual_length=36,...</div></div></div></a><a class="pagination-related" href="/articles/shardingsphere/ShardingSphere-inline%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%97%AE%E9%A2%98%E5%AE%9A%E4%BD%8D.html" title="ShardingSphere-inline表达式线程安全问题定位"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-27</div><div class="info-item-2">ShardingSphere-inline表达式线程安全问题定位</div></div><div class="info-2"><div class="info-item-1">ShardingSphere inline表达式线程安全问题定位问题背景春节期间发现 ShardingSphere 事务 E2E 偶发执行失败问题，并且每次执行失败需要执行很久，直到超时。最终定位发现 inline 表达式存在线程安全问题。本文记录定位并解决 inline 表达式线程安全问题的过程。 问题原因1.GroovyInlineExpressionParser 里有成员变量，存在并发修改，不能使用单例 SPI 实现;2.执行 Groovy 表达式时，需要执行 rehydrate 方法 copy Closure,使得每个 Closure 都有独立的执行环境，避免属性赋值时产生线程安全问题。 问题定位构造测试用例尝试复现问题构造测试用例，且在测试用例中添加线程相关信息，观察执行结果。 1234567891011121314151617181920212223242526272829@Test@SneakyThrows(&#123;ExecutionException.class, InterruptedException.class&#125;)void...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#ShardingSphere-SQL-%E8%A7%A3%E6%9E%90-Issue-%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B"><span class="toc-number">1.</span> <span class="toc-text">ShardingSphere-SQL 解析 Issue 处理流程</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Issue-%E8%83%8C%E6%99%AF%E8%AF%B4%E6%98%8E"><span class="toc-number">1.1.</span> <span class="toc-text">Issue 背景说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E4%BD%93%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">总体流程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ANTLR-%E8%AF%AD%E6%B3%95%E8%A7%A3%E6%9E%90%E9%97%AE%E9%A2%98%E4%BF%AE%E5%A4%8D"><span class="toc-number">1.2.1.</span> <span class="toc-text">ANTLR 语法解析问题修复</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Visitor-%E9%97%AE%E9%A2%98%E4%BF%AE%E5%A4%8D"><span class="toc-number">1.2.2.</span> <span class="toc-text">Visitor 问题修复</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%96%AD%E8%A8%80%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.3.</span> <span class="toc-text">添加断言测试文件</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>